package com.threeti.jiayusms.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.threeti.jiayusms.domain.model.legacy.CommunicationLog;
import com.threeti.jiayusms.domain.util.DateUtil;



@Controller
public class ExportSmsLogController {


	@RequestMapping("/exportSmsHistory")
	public static ModelAndView exportSmsHistory(HttpServletRequest request,
			HttpServletResponse response) throws IOException, WriteException {
		List<CommunicationLog> LogTemplatesList = (List<CommunicationLog>) request.getAttribute("export");
		// String title = request.getParameter("betbackHistory");
		String fileName = "SMSHISTORY.xls";
//		List<Trans> transes = betbackHistoryService.getAllTrans();
//		StringBuilder sb = new StringBuilder();
//		sb.append("顺序号,");
//		sb.append("通信日期,");
//		sb.append("通信编号,");
//		sb.append("客户名称,");
//		sb.append("发车单号,");
//		sb.append("运单号,");
//		sb.append("通信方式,");
//		sb.append("通信号码,");
//		sb.append("通信内容,");
//		sb.append("通信状态,");
//		sb.append("发送时间,");
//		sb.append("分公司,");
//		sb.append("操作员\n");
//		sb.append("1,");
//		sb.append("2,");
//		sb.append("3,");
//		sb.append("4,");
//		sb.append("5,");
//		sb.append("6,");
//		sb.append("7,");
//		sb.append("8,");
//		sb.append("9,");
//		sb.append("10,");
//		sb.append("11,");
//		sb.append("12,");
//		sb.append("13\n");
//		if(LogTemplatesList != null){
//			for (CommunicationLog log : LogTemplatesList) {
//				sb.append(log.getId() + ",");
//				sb.append( DateUtil.toString(log.getCommunicationDate()) == null ? " " : DateUtil.toString(log.getCommunicationDate()) + ",");
//				sb.append( log.getId() == null ? " " :  log.getId() + ",");
//				sb.append( log.getClientName() == null ? " " : log.getClientName() + ",");
//				sb.append( log.getTruckId() == null ? " " : log.getTruckId() + ",");
//				sb.append( log.getCarryId() == null ? " " : log.getCarryId() + ",");
//				sb.append( log.getCommunicationMode() == null ? " " : log.getCommunicationMode() + ",");
//				sb.append( log.getCommunicationMode() == null ? " " : log.getCommunicationMode() + ",");
//				sb.append( log.getCommunicationContent() == null ? " " : log.getCommunicationContent() + ",");
//				sb.append( log.getCommunicationFlag() + ",");
//				sb.append( DateUtil.toString(log.getCommunicationTime()) == null ? " " : DateUtil.toString(log.getCommunicationTime()) + ",");
//				sb.append( log.getBdepartment() == null ? " " : log.getBdepartment() + ",");
//				sb.append( log.getModifyUser() == null ? " " : log.getModifyUser() + "\n");
//			}
//		}
		
		try {
			response.reset();
			response.setHeader("Content-Disposition", "attachment;filename="
					+ URLEncoder.encode(fileName, "UTF-8"));
			response.setContentType("application/octet-stream");
			response.setCharacterEncoding("GBK");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());
  	WritableSheet sheet = workbook.createSheet("SMSlog", 0);
  	WritableFont font = new WritableFont(WritableFont.TIMES, 11);
  	font.setColour(Colour.BLACK);
  	WritableCellFormat cellFormat = new WritableCellFormat();
  	cellFormat.setFont(font);
  	cellFormat.setAlignment(Alignment.CENTRE);
  	cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.GRAY_80);
  	Label l1 = new Label(0, 0, "顺序号", cellFormat);
  	Label l2 = new Label(1, 0, "通信日期", cellFormat);
  	Label l3 = new Label(2, 0, "通信编号", cellFormat);
  	Label l4 = new Label(3, 0, "客户名称", cellFormat);
  	Label l5 = new Label(4, 0, "发车单号", cellFormat);
  	Label l6 = new Label(5, 0, "运单号", cellFormat);
  	Label l7 = new Label(6, 0, "通信方式", cellFormat);
  	Label l8 = new Label(7, 0, "通信号码", cellFormat);
  	Label l9 = new Label(8, 0, "通信内容", cellFormat);
  	Label l10 = new Label(9, 0, "通信状态", cellFormat);
  	Label l11 = new Label(10, 0, "发送时间", cellFormat);
  	Label l12 = new Label(11, 0, "分公司", cellFormat);
  	Label l13 = new Label(12, 0, "操作员", cellFormat);
  	
  	sheet.addCell(l1);
  	sheet.addCell(l2);
  	sheet.addCell(l3);
  	sheet.addCell(l4);
  	sheet.addCell(l5);
  	sheet.addCell(l6);
  	sheet.addCell(l7);
  	sheet.addCell(l8);
  	sheet.addCell(l9);
  	sheet.addCell(l10);
  	sheet.addCell(l11);
  	sheet.addCell(l12);
  	sheet.addCell(l13);
  	
  	if(LogTemplatesList != null){
	  	for(int i=1; i<=LogTemplatesList.size(); i++) {
	  		CommunicationLog log = LogTemplatesList.get(i-1);
	  		List list = new ArrayList();
	  		list.add(String.valueOf(log.getId()));
	  		list.add(DateUtil.toString(log.getCommunicationDate()) == null ? " " : DateUtil.toString(log.getCommunicationDate()));
	  		list.add(log.getCustomId() == null ? " " :  log.getCustomId());
	  		list.add(log.getClientName() == null ? " " : log.getClientName());
	  		list.add(log.getTruckId() == null ? " " : log.getTruckId());
	  		list.add(log.getCarryId() == null ? " " : log.getCarryId());
	  		String s = log.getCommunicationMode() == null ? " " : log.getCommunicationMode();
	  		if("sms".equals(s))list.add("短信");
	  		else if("voice".equals(s)) list.add("语音");
	  		else list.add(s);
	  		list.add(log.getCommunicationNumber() == null ? " " : log.getCommunicationNumber());
	  		list.add(log.getCommunicationContent() == null ? " " : log.getCommunicationContent());
	  		String s2 = log.getCommunicationFlag() ==null ? " " : log.getCommunicationFlag();
	  		if("S00".equals(s2)) list.add("未发送");
	  		else if("S0B".equals(s2)) list.add("正在发送");
	  		else if("S0S".equals(s2)) list.add("发送完成");
	  		else list.add(s2);
	  		list.add(DateUtil.toString(log.getCommunicationTime()) == null ? " " : DateUtil.toString(log.getCommunicationTime()));
	  		list.add(log.getBdepartment() == null ? " " : log.getBdepartment());
	  		list.add(log.getModifyUser() == null ? " " : log.getModifyUser());
	  		for(int k=0; k<list.size(); k++) {
	  			Label label = new Label(k, i, (String)list.get(k), cellFormat);
	  			sheet.addCell(label);
	  		}	  		
	  	}
  	}
  	workbook.write();
		workbook.close();
		
		return null;
	}
}
